package com.yulongtian.offers.day202208.day20220823;

import java.util.Stack;

/**
 * @author YulongTian
 * @date 2022/8/23 - 12:53
 */
public class JianZhiOffer02 {
    public static void main(String[] args) {

    }
}

class MinStack {

    private Stack<Integer[]> stack = new Stack<>();

    private Integer currentMin = Integer.MAX_VALUE;

    /**
     * initialize your data structure here.
     */
    public MinStack() {

    }

    public void push(int x) {
        if (!stack.empty() && currentMin >= x) {
            currentMin = x;
        }
        if (stack.empty()) {
            currentMin = x;
        }
        stack.push(new Integer[]{x, currentMin});
    }

    public void pop() {
        Integer[] peek = stack.peek();

        stack.pop();
        if (peek[0].equals(peek[1])) {
            if (!stack.empty()) {
                currentMin = stack.peek()[1];
            }else{
                currentMin=Integer.MAX_VALUE;
            }
        }
    }

    public int top() {
        return stack.peek()[0];
    }

    public int min() {
        return currentMin;
    }
}
